package com.hm.health.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.hm.health.R;
import com.hm.health.entity.DeviceConfigBean;

import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by chenshi on 2020/4/8.
 */

public class DeviceConfigAdapter extends RecyclerView.Adapter<DeviceConfigAdapter.ViewHolder> {
    private int mLayoutResId;
    private Context mContext;
    private List<DeviceConfigBean> mDeviceConfigBeans;
    private String[] mIds;
    private boolean mShowProgressBar = true;

    public DeviceConfigAdapter(Context context, List<DeviceConfigBean> deviceConfigBeans) {
        mContext = context;
        mDeviceConfigBeans = deviceConfigBeans;
        mIds = new String[]{"0","1", "2", "3", "4", "5", "6", "7"};
        mLayoutResId = R.layout.item_device_config;
    }

    public DeviceConfigAdapter(Context context, List<DeviceConfigBean> deviceConfigBeans, int layoutId) {
        this(context, deviceConfigBeans);
        mLayoutResId = layoutId;
    }

    public void showProgressBar(boolean isShow) {
        mShowProgressBar = isShow;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(mLayoutResId,null);
        ViewHolder holder = new ViewHolder(view);
        holder.mProgressBar.setVisibility(mShowProgressBar ? View.VISIBLE : View.GONE);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(mContext,
                android.R.layout.simple_spinner_item, mIds);
        holder.mSpConfigId.setAdapter(arrayAdapter);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DeviceConfigBean data = mDeviceConfigBeans.get(position);
        holder.mCbIsSelected.setChecked(data.isSelected());
        holder.mTvFileName.setText(data.getFileName());
        holder.mProgressBar.setText(String.format(Locale.ENGLISH, "%4d %%", data.getProgress()));

       holder.mCbIsSelected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
               data.setSelected(b);
           }
       });

        holder.mSpConfigId.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                data.setId(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        switch (data.getStatus()) {
            case DeviceConfigBean.STATUS_READY:
                holder.mTvState.setText("Ready");
                holder.mTvState.setTextColor(Color.BLACK);
                break;
            case DeviceConfigBean.STATUS_SENDING:
                holder.mTvState.setText("Executing");
                holder.mTvState.setTextColor(mContext.getResources().getColor(R.color.michaelapp_orange));
                break;
            case DeviceConfigBean.STATUS_SUCCESS:
                holder.mTvState.setText("Finished");
                holder.mTvState.setTextColor(Color.GREEN);
                break;
            case DeviceConfigBean.STATUS_ERROR:
                holder.mTvState.setText("Error");
                holder.mTvState.setTextColor(Color.RED);
                break;
        }
    }

    @Override
    public int getItemCount() {
        if (null != mDeviceConfigBeans) {
            return mDeviceConfigBeans.size();
        } else {
            return 0;
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.sp_config_id)
        Spinner mSpConfigId;
        @BindView(R.id.tv_file_name)
        TextView mTvFileName;
        @BindView(R.id.cb_selected)
        CheckBox mCbIsSelected;
        @BindView(R.id.tv_state)
        TextView mTvState;
        @BindView(R.id.pb_percent)
        TextView mProgressBar;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
